package poradnia

class WizytaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(controller: "pacjent", action: "list", params: params)
    }

//    def list = {
//        params.max = Math.min(params.max ? params.int('max') : 10, 100)
//        [wizytaInstanceList: Wizyta.list(params), wizytaInstanceTotal: Wizyta.count()]
//    }

    def create = {
        def wizytaInstance = new Wizyta()
        def pacjent = Pacjent.get(params.pacjent.id)
        def count = pacjent.wizyta.size()
        wizytaInstance.numerWizyty = count ? count+1 : 1
        wizytaInstance.properties = params
        return [wizytaInstance: wizytaInstance]
    }

    def save = {
        def wizytaInstance = new Wizyta(params)
        if (wizytaInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'wizyta.label', default: 'Wizyta'), wizytaInstance.numerWizyty])}"
            redirect(action: "show", id: wizytaInstance.id)
        }
        else {
            render(view: "create", model: [wizytaInstance: wizytaInstance])
        }
    }

    def show = {
        def wizytaInstance = Wizyta.get(params.id)
        if (!wizytaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'wizyta.label', default: 'Wizyta'), params.id])}"
            redirect(action: "list")
        }
        else {
            [wizytaInstance: wizytaInstance]
        }
    }

    def edit = {
        def wizytaInstance = Wizyta.get(params.id)
        if (!wizytaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'wizyta.label', default: 'Wizyta'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [wizytaInstance: wizytaInstance]
        }
    }

    def update = {
        def wizytaInstance = Wizyta.get(params.id)
        if (wizytaInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (wizytaInstance.version > version) {
                    
                    wizytaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'wizyta.label', default: 'Wizyta')] as Object[], "Another user has updated this Wizyta while you were editing")
                    render(view: "edit", model: [wizytaInstance: wizytaInstance])
                    return
                }
            }
            wizytaInstance.properties = params
            if (!wizytaInstance.hasErrors() && wizytaInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'wizyta.label', default: 'Wizyta'), wizytaInstance.numerWizyty])}"
                redirect(action: "show", id: wizytaInstance.id)
            }
            else {
                render(view: "edit", model: [wizytaInstance: wizytaInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'wizyta.label', default: 'Wizyta'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def wizytaInstance = Wizyta.get(params.id)
        if (wizytaInstance) {
            try {
		def pacjentId = wizytaInstance.pacjent.id
                wizytaInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'wizyta.label', default: 'Wizyta'), params.id])}"
                redirect(controller: "pacjent", action: "show", id: pacjentId)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'wizyta.label', default: 'Wizyta'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'wizyta.label', default: 'Wizyta'), params.id])}"
            redirect(controller: "pacjent", action: "index")
        }
    }
}
